package cn.itcast.bos.delivery.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb.OrderDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.base.service.FixedAreaService;
import cn.itcast.bos.base.service.SubAreaService;
import cn.itcast.bos.delivery.dao.OrderDao;
import cn.itcast.bos.delivery.service.OrderService;
import cn.itcast.bos.delivery.service.WayBillService;
import cn.itcast.bos.delivery.service.WorkBillService;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.domain.delivery.Order;
import cn.itcast.crm.domain.Customer;

@Service
@Transactional
public class OrderServiceImpl implements OrderService{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private FixedAreaService fixedAreaService;
	@Autowired
	private SubAreaService subAreaService;
	@Autowired
	private WorkBillService workBillService;
	@Autowired
	private WayBillService wayBillService;
	@Autowired
	private OrderDao orderdao;
	@Autowired
	private sendMsg sendmsg;
	@Override
	public void getOrder(Order order) {
		
		System.out.println(order);
		Customer customer = WebClient.create("http://localhost:8082/crm_management/service/crm/customerservice/customer/id/"+order.getCustomer_id()).
				accept(MediaType.APPLICATION_JSON).get(Customer.class);
		//当订单地址与用户地址相同时
		logger.info("客户id"+customer.getId());
		if(StringUtils.isNotBlank(order.getSendAddress())&&StringUtils.isNotBlank(customer.getAddress())){
			logger.info("order地址与customer地址都不为空");
			if(order.getSendAddress().contains(customer.getAddress())){
				logger.info("客户下单地址与客户所填地址匹配");
				//获取定区byId
				FixedArea fixedArea = fixedAreaService.findById(customer.getFixedAreaId());
				//获取快递员
				Set<Courier> couriers = fixedArea.getCouriers();
				Courier courier = couriers.iterator().next();
				order.setCourier(courier);
				//自动分单
				order.setOrderType("1");
				//保存订单前需要保存工单与运单
				saveBills(order, courier);
				orderdao.save(order);
				send(courier.getTelephone(),order.getSendMobileMsg());
				return ;
			}
		}
			
		//当用户地址与订单地址不同时通过区域查找分区-定区-快递员
		SubArea subArea = subAreaService.getSubArea(order.getSendArea(),order);
		//根据分区获得定区
		if(subArea!=null){
			//根据分区获取定区
			FixedArea fixedArea = subArea.getFixedArea();
			logger.info("定区id为:"+fixedArea.getId());
			//获取快递员
			Set<Courier> couriers = fixedArea.getCouriers();
			if(couriers.size()>0){
				Courier courier = couriers.iterator().next();
				order.setCourier(courier);
				//自动分单
				order.setOrderType("1");
				orderdao.save(order);
				//保存订单前需要保存工单与运单
				saveBills(order, courier);
				send(courier.getTelephone(),order.getSendMobileMsg());
				return ;
			}else{
				logger.info("没找到快递员设置手动下订单");
				order.setOrderType("2");
				orderdao.save(order);
			}
		}
		//没有找到快递员手动分单
		logger.info("设置手动下订单");
		order.setOrderType("2");
		orderdao.save(order);
	}

	private void saveBills(Order order, Courier courier) {
		workBillService.save(order,courier);
	//	wayBillService.save(order,courier);
	}
	
	private void send(String telephone,String msg){
		Map<String,String> map=new HashMap<String,String>();
		map.put("telephone", telephone);
		map.put("code", msg);
		sendmsg.send("sendmsg", map);
	}

	@Override
	public Order findByOrderNum(String orderNum) {
		// TODO Auto-generated method stub
		Order order= orderdao.findByOrderNum(orderNum);
		logger.info(order.toString());
		return order;
	}

}
